/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
package com.shipwe.loans.weight.mask;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.shipwe.loans.utils.AppDensityUtil;

/**
 * MaskFaceView
 * @Author wwdeng
 * @CreateTime: 2024/11/22 11:08
 * @Annotation：人脸拍照取景框
 */
@SuppressWarnings("unused")
public class ShipweMaskFaceView extends View {

    public void setLineColor(int lineColor) {
        this.lineColor = lineColor;
    }

    public void setMaskColor(int maskColor) {
        this.maskColor = maskColor;
    }

    private int lineColor = Color.parseColor("#FF7543FF");

    private int maskColor = Color.rgb(0, 0, 0);

    private final Paint framePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Rect frameRect = new Rect();

    private Drawable locatorDrawable;

    public Rect getFrameRect() {
        return new Rect(frameRect);
    }

    public void setOrientation(int orientation) {
    }

    public ShipweMaskFaceView(Context context) {
        super(context);
        init();
    }

    public ShipweMaskFaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShipweMaskFaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (w > 0 && h > 0) {
            float ratio = h > w ? 0.9f : 0.72f;

            int width = w - AppDensityUtil.dp2px(100);

            int left = (w - width) / 2;
            int top = (h - width) / 3;
            int right = width + left;
            int bottom = width + top;

            frameRect.left = left;
            frameRect.top = top;
            frameRect.right = right;
            frameRect.bottom = bottom;

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = frameRect.width();
        int height = frameRect.height();

        int left = frameRect.left;
        int top = frameRect.top;
        int right = frameRect.right;
        int bottom = frameRect.bottom;

        canvas.drawColor(maskColor);
        fillRectRound(left, top, right, bottom);
        canvas.drawPath(path, paint);
        canvas.drawPath(path, framePaint);
    }

    private Path path = new Path();

    private void fillRectRound(float left, float top, float right, float bottom) {
        path.reset();
        float width = right - left;
        float height = bottom - top;
        float rx = width / 2;
        float ry = height / 2;

        // 添加一个圆形路径到Path对象
        float centerX = width / 2;
        float centerY = height / 2;
        float radius = Math.min(centerX, centerY); // 使半径为视图较小的一半
        path.addCircle(frameRect.left + centerX, frameRect.top + centerY, radius, Path.Direction.CW);
        path.close();
    }

    {
        // 硬件加速不支持，图层混合。
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        paint.setColor(lineColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(AppDensityUtil.dp2px(10));

        framePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }
}
